package com.chzc.fm.duotin.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.FileInfo;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.db.ContentDao;
import com.chzc.fm.duotin.ui.mine.service.DownloadService;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.LogUtil;

import java.io.File;

/**
 * Created by BillSinla on 2016/7/3.
 */
public class SpecialItem extends RelativeLayout{
    private RelativeLayout rlContent;
    private TextView textTitle, textDes;
    private ImageView imageLoad;
    private SpecialBean.ContentListBean content;
    private OnItemUpdateListener onItemUpdateListener;
    private ContentDao contentDao;

    public SpecialItem(Context context, SpecialBean.ContentListBean content) {
        super(context);
        this.content = content;
        init(context);
        setContent(content);
    }

    public SpecialItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(final Context context) {
        inflate(context, R.layout.widget_special_item, this);

        contentDao = new ContentDao(context);

        rlContent = (RelativeLayout) findViewById(R.id.rl_special_item);
        textTitle = (TextView) findViewById(R.id.tv_special_title);
        textDes = (TextView) findViewById(R.id.tv_special_des);
        imageLoad = (ImageView) findViewById(R.id.iv_special_load);

        imageLoad.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imageLoad.setImageResource(R.drawable.anim_download);
                AnimationDrawable animation = (AnimationDrawable) imageLoad.getDrawable();
                animation.start();

                File file = new File(FileUtils.DIR_MP3, content.getTitle());
                FileInfo fileInfo = new FileInfo(Integer.parseInt(content.getId()),
                        content.getAudio_64_url(),
                        file.toString(),
                        Long.parseLong(content.getAudio_64_size()), 0);

                if(!contentDao.isExist(content.getId())) {
                    contentDao.insertContent(content);
                    if (onItemUpdateListener != null)
                        onItemUpdateListener.update();
                }else {
                    Toast.makeText(context, "数据库已经存在该数据", Toast.LENGTH_SHORT).show();
                }



                Intent intent = new Intent(context, DownloadService.class);
                intent.setAction(DownloadService.ACTION_START);
                intent.setPackage(context.getPackageName());
                intent.putExtra("fileInfo", fileInfo);
                context.startService(intent);

                imageLoad.setOnClickListener(null);
            }
        });

    }

    public void setOnItemUpdateListener(OnItemUpdateListener onItemUpdateListener) {
        this.onItemUpdateListener = onItemUpdateListener;
    }

    public void setContent(final SpecialBean.ContentListBean content) {
        this.content = content;
        textTitle.setText(content.getTitle());
        textDes.setText("更新时间：" + content.getUpdated());

        rlContent.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.putExtra("id", content.getId());
            }
        });
    }

    interface OnItemUpdateListener{
        void update();
    }
}
